package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.GzdsGrade;
import com.ruoyi.system.domain.GzdsQuestionInfo;
import com.ruoyi.system.service.IGzdsGradeService;
import com.ruoyi.system.service.IGzdsQuestionInfoService;
import com.ruoyi.system.utils.CatchUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 试题管理Controller
 *
 * @author ruoyi
 * @date 2021-04-20
 */
@RestController
@RequestMapping("/system/bank")
public class GzdsQuestionInfoController extends BaseController
{
    @Autowired
    private IGzdsQuestionInfoService gzdsQuestionInfoService;

    @Autowired
    private  IGzdsGradeService gzdsGradeService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询试题管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:bank:list')")
    @GetMapping("/list")
    public TableDataInfo list(GzdsQuestionInfo gzdsQuestionInfo)
    {
        startPage();
        List<GzdsQuestionInfo> list = gzdsQuestionInfoService.selectGzdsQuestionInfoList(gzdsQuestionInfo);
        return getDataTable(list);
    }

    /**
     * 导出试题管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:bank:export')")
    @Log(title = "试题管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GzdsQuestionInfo gzdsQuestionInfo)
    {
        List<GzdsQuestionInfo> list = gzdsQuestionInfoService.selectGzdsQuestionInfoList(gzdsQuestionInfo);
        ExcelUtil<GzdsQuestionInfo> util = new ExcelUtil<GzdsQuestionInfo>(GzdsQuestionInfo.class);
        return util.exportExcel(list, "试题管理数据");
    }

    /**
     * 获取试题管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:bank:query')")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") Long questionId)
    {
        return AjaxResult.success(gzdsQuestionInfoService.selectGzdsQuestionInfoById(questionId));
    }

    /**
     * 新增试题管理
     */
    @PreAuthorize("@ss.hasPermi('system:bank:add')")
    @Log(title = "试题管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GzdsQuestionInfo gzdsQuestionInfo)
    {
        return toAjax(gzdsQuestionInfoService.insertGzdsQuestionInfo(gzdsQuestionInfo));
    }

    /**
     * 修改试题管理
     */
    @PreAuthorize("@ss.hasPermi('system:bank:edit')")
    @Log(title = "试题管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GzdsQuestionInfo gzdsQuestionInfo)
    {
        return toAjax(gzdsQuestionInfoService.updateGzdsQuestionInfo(gzdsQuestionInfo));
    }

    /**
     * 删除试题管理
     */
    @PreAuthorize("@ss.hasPermi('system:bank:remove')")
    @Log(title = "试题管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds)
    {
        return toAjax(gzdsQuestionInfoService.deleteGzdsQuestionInfoByIds(questionIds));
    }

    /**
     * 海选题库
     */
    @ApiOperation("题库")
    @GetMapping( "/mixinfo")
    @ResponseBody
    public AjaxResult mixinfo() {
        AjaxResult json = new AjaxResult();
        List<GzdsQuestionInfo> listZero = new ArrayList<GzdsQuestionInfo>();
        List<GzdsQuestionInfo> listOne = new ArrayList<GzdsQuestionInfo>();
        List<GzdsQuestionInfo> listTwo = new ArrayList<GzdsQuestionInfo>();
        listZero = gzdsQuestionInfoService.selectGzdsQuestionInfoZero();
        listOne = gzdsQuestionInfoService.selectGzdsQuestionInfoOne();
        listTwo = gzdsQuestionInfoService.selectGzdsQuestionInfoTwo();
        json.put("单选",listZero);
        json.put("多选",listOne);
        json.put("判断",listTwo);
        return json;
    }

    /**
     * 复赛题库
     */
    @ApiOperation("复赛题库")
    @GetMapping( "/midinfo")
    @ResponseBody
    public AjaxResult midinfo() {
        AjaxResult json = new AjaxResult();
        List<GzdsQuestionInfo> listZero = new ArrayList<GzdsQuestionInfo>();
        List<GzdsQuestionInfo> listOne = new ArrayList<GzdsQuestionInfo>();
        List<GzdsQuestionInfo> listTwo = new ArrayList<GzdsQuestionInfo>();
        listZero = gzdsQuestionInfoService.selectGzdsQuestionInfoMidZero();
        listOne = gzdsQuestionInfoService.selectGzdsQuestionInfoMidOne();
        listTwo = gzdsQuestionInfoService.selectGzdsQuestionInfoMidTwo();
        json.put("单选",listZero);
        json.put("多选",listOne);
        json.put("判断",listTwo);
        return json;
    }

    /**
     * 决赛题库
     */
    @ApiOperation("决赛题库")
    @GetMapping( "/maxinfo")
    @ResponseBody
    public AjaxResult maxinfo(String phoneNumber, String value) {
        /** 待返回数据 **/
        AjaxResult json = new AjaxResult();
        if (StringUtils.isEmpty(phoneNumber) || StringUtils.isEmpty(value)) {
            json.put("code", "500");
            json.put("errorMsg", "参数传输错误");
            return json;
        }

        /** 获取缓存 **/
        String catchValue = CatchUtils.getCache(phoneNumber);
        if (StringUtils.isEmpty(catchValue) || StringUtils.isEmpty(value) || !StringUtils.equals(catchValue, value)) {
            json.put("code", "500");
            json.put("errorMsg", "用户已获取试题，答题结束!");
        } else {
            List<GzdsQuestionInfo> listZero = gzdsQuestionInfoService.selectGzdsQuestionInfoMaxZero();
            List<GzdsQuestionInfo> listOne = gzdsQuestionInfoService.selectGzdsQuestionInfoMaxOne();
            List<GzdsQuestionInfo> listTwo = gzdsQuestionInfoService.selectGzdsQuestionInfoMaxTwo();
            json.put("单选",listZero);
            json.put("多选",listOne);
            json.put("判断",listTwo);
            CatchUtils.removeCache(phoneNumber);
        }

        /** 返回数据 **/
        return json;
    }

    /**
     * 试题导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "试题管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:bank:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GzdsQuestionInfo> util = new ExcelUtil<>(GzdsQuestionInfo.class);
        List<GzdsQuestionInfo> questionInfoList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = gzdsQuestionInfoService.importQuestion(questionInfoList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 试题模板下载
     *
     * @return
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<GzdsQuestionInfo> util = new ExcelUtil<>(GzdsQuestionInfo.class);
        return util.importTemplateExcel("试题数据");
    }

    private String checkout(String banklevel,String name,String phoneNumber){
        String msg = new String();
        if(banklevel.equals("海选")){
            List<GzdsGrade> gzdsGradeList =  gzdsGradeService.selectGzdsGradeNameNumber(banklevel,name,phoneNumber);
               if(gzdsGradeList.size()==1&&(gzdsGradeList.get(0).getPerformance())==null){
                   //0 可以考试
                   msg = "0";
               }else {
                   //1 不能考试
                   msg = "1";
               }
        } else if(banklevel.equals("复赛")){

                List<GzdsGrade> gzdsGradeList = gzdsGradeService.selectGzdsGradeNameNumber(banklevel,name,phoneNumber);
                if(gzdsGradeList.size()==1&&(gzdsGradeList.get(0).getPerformance())==null){
                    msg = "1";
                }else {
                    banklevel = "海选";
                    gzdsGradeList = gzdsGradeService.selectGzdsGradeNameNumber(banklevel,name,phoneNumber);
                    if((gzdsGradeList.get(0).getPerformance())==null){
                        msg = "1";
                    }else {
                        msg = "0";
                    }
                }
        }else if(banklevel.equals("决赛")){
            List<GzdsGrade> gzdsGradeList = gzdsGradeService.selectGzdsGradeNameNumber(banklevel,name,phoneNumber);
            if(gzdsGradeList.size()==1&&(gzdsGradeList.get(0).getPerformance())==null){
                msg = "1";
            }else {
                banklevel = "复赛";
                gzdsGradeList = gzdsGradeService.selectGzdsGradeNameNumber(banklevel,name,phoneNumber);
                if(gzdsGradeList.size()==1&&(gzdsGradeList.get(0).getPerformance())==null){
                    msg = "2";
                }else if (gzdsGradeList.size()==1&&(gzdsGradeList.get(0).getPerformance())!=null){
                    msg = "0";
                }
            }
        }else {
            msg = "2";
        }
        return msg;
    }
}
